/*
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 *
 * Copyright @2008 the original author or authors.
 */
package com.bluestone.scripts;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

import com.bluestone.util.Util;

/**
 * A function can accept some parameters and some actions when be refered by
 * other action.
 * 
 * @author <a href="mailto:bluesotne.master@gmail.com">daniel.q</a>
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "function", propOrder = { "script", "parent" })
public class Function {
	@XmlAttribute
	private String name = null;

	@XmlElement(name = "script")
	private String script = null;

	@XmlElement(name = "parent")
	private Object parent = null;

	/**
	 * Defines the name of the funcion
	 * 
	 * @return name - String
	 */
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Function contents in the format of the XML
	 * 
	 * @return
	 */
	public String getScript() {
		return script;
	}

	public void setScript(String script) {
		this.script = script;
	}

	public Object getParent() {
		return parent;
	}

	public void setParent(Object parent) {
		this.parent = parent;
	}

	/**
	 * Compile the script of the function
	 * 
	 * @param map
	 *            HashMap
	 * @return actions - ArrayList
	 */
	public ArrayList compile(HashMap map) {
		String temp = script;
		temp = temp.replace("&", "&amp;");
		//temp = Util.markupTags(temp);
		ArrayList actions = new ArrayList();
		Iterator iterator = map.keySet().iterator();
		while (iterator.hasNext()) {
			String key = (String) iterator.next();
			String value = (String) map.get(key);
			//value = value.replace("\"", "&quot;");
			value = Util.markupTags(value);
			value = value.replace("\\","\\\\");
			temp = temp.replaceAll("\\{" + key + "\\}", value);

		}
		temp = Util.getDynamicString(temp);
		temp = "<script>" + temp + "</script>";
		try {
			JAXBContext context = JAXBContext.newInstance(Script.class);
			Unmarshaller unmarshaller = context.createUnmarshaller();
			Script obj = (Script) unmarshaller
					.unmarshal(new java.io.StringReader(temp));
			actions = (ArrayList)obj.getActions();
		} catch (Exception me) {
			Util.getLogger().error(me);
			Util.getLogger().error(temp);
		}
		return actions;
	}

}
